
/*这段代码是固定的，必须要放到js中*/
function setupWebViewJavascriptBridge(callback) {
    if (window.WebViewJavascriptBridge) {
        return callback(WebViewJavascriptBridge);
    }
    if (window.WVJBCallbacks) {
        return window.WVJBCallbacks.push(callback);
    }
    window.WVJBCallbacks = [callback];
    var WVJBIframe = document.createElement('iframe');
    WVJBIframe.style.display = 'none';
    WVJBIframe.src = 'wvjbscheme://__bridge_loaded__';
    // WVJBIframe.src = 'wvjbscheme://__BRIDGE_LOADED__';
    document.documentElement.appendChild(WVJBIframe);
    setTimeout(function () {
        document.documentElement.removeChild(WVJBIframe)
    }, 0)
}

/*与OC交互的所有JS方法都要放在此处注册，才能调用通过JS调用OC或者让OC调用这里的JS*/
if (!!navigator.userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/)) {
    setupWebViewJavascriptBridge(function (bridge) {
        window.ios = bridge
        var uniqueId = 1
        function log(message, data) {
            var log = document.getElementById('log')
            var el = document.createElement('div')
            el.className = 'logLine'
            el.innerHTML = uniqueId++ + '. ' + message + ':<br/>' + JSON.stringify(data)
            if (log.children.length) {
                log.insertBefore(el, log.children[0])
            } else {
                log.appendChild(el)
            }
        }
    })
}

var AppCommonUtils = {
    browser: {
        trident: navigator.userAgent.indexOf('Trident') > -1, //IE内核
        presto: navigator.userAgent.indexOf('Presto') > -1, //opera内核
        webKit: navigator.userAgent.indexOf('AppleWebKit') > -1, //苹果、谷歌内核
        gecko: navigator.userAgent.indexOf('Gecko') > -1 && navigator.userAgent.indexOf('KHTML') == -1,//火狐内核
        mobile: !!navigator.userAgent.match(/AppleWebKit.*Mobile.*/), //是否为移动终端
        ios: !!navigator.userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端
        android: navigator.userAgent.indexOf('Android') > -1 || navigator.userAgent.indexOf('Adr') > -1, //android终端
        iPhone: navigator.userAgent.indexOf('iPhone') > -1, //是否为iPhone或者QQHD浏览器
        iPad: navigator.userAgent.indexOf('iPad') > -1, //是否iPad
        webApp: navigator.userAgent.indexOf('Safari') == -1, //是否web应该程序，没有头部与底部
        weixin: navigator.userAgent.indexOf('MicroMessenger') > -1, //是否微信 （2015-01-22新增）
        qq: navigator.userAgent.match(/\sQQ/i) == " qq" //是否QQ
    },
};

var AppBridgeGlobal = {
    ready(callback){
        var isBridge = false;
        var bridgeSetTimeout = null;
        var bridgeNum = 0;
        function onloadFun() {
            AppBridgeGlobal.getHeadInfo(function () {
                if (isBridge){
                    return false
                }
                clearTimeout(bridgeSetTimeout);
                isBridge = true;
                callback();
            });
            bridgeSetTimeout = setTimeout(function () {
                if(bridgeNum>2){
                    return false
                }
                onloadFun();
                bridgeNum ++
            },2000);
        }
        setTimeout(function () {
            onloadFun();
        },500);
    },
    // 退出app
    exitApp() {
        console.log('call exitApp');
        if (AppCommonUtils.browser.android) {
            let option = {
                fcName: 'exitApp',
                data: {}
            };
            window.android && window.android.postMessage(JSON.stringify(option));
        } else {
            window.ios && window.ios.callHandler("exitApp", {});
        }
    },
    // save通知函数名
    setNoticeFunName(data, callback) {
        console.log('call setNoticeFunName');
        if (AppCommonUtils.browser.android) {
            window.setNoticeFunNameCallback = callback;
            let option = {
                fcName: 'setNoticeFunName',
                data: data
            }
            window.android && window.android.postMessage(JSON.stringify(option));
        } else {
            window.ios && window.ios.callHandler("setNoticeFunName", data, callback);
        }
    },
    // 调用分享
    goToShare(data, callback) {
        console.log('call goToShare');
        if (AppCommonUtils.browser.android) {
            window.goToShareCallback = callback;
            let option = {
                fcName: 'goToShare',
                data: data
            }
            window.android && window.android.postMessage(JSON.stringify(option));
        } else {
            window.ios && window.ios.callHandler("goToShare", data, callback);
        }
    },
    // 组件首页点击返回
    homeBackClicked() {
        console.log('call homeBackClicked');
        if (AppCommonUtils.browser.android) {
            let option = {
                fcName: 'homeBackClicked',
                data: {}
            }
            window.android && window.android.postMessage(JSON.stringify(option));
        } else {
            window.ios && window.ios.callHandler("homeBackClicked", {});
        }
    },
    // 获取当前标题栏显示
    getTitleBarParam(callback) {
        console.log('call getTitleBarParam');
        if (AppCommonUtils.browser.android) {
            window.getTitleBarParamCallback = callback;
            let option = {
                fcName: 'getTitleBarParam',
                data: {}
            };
            window.android && window.android.postMessage(JSON.stringify(option));
        } else {
            window.ios && window.ios.callHandler("getTitleBarParam", {}, callback);
        }
    },

    // 获取组件首页页面级别
    getHomePageLevel(callback) {
        console.log('call getHomePageLevel');
        if (AppCommonUtils.browser.android) {
            window.getHomePageLevelCallback = callback;
            let option = {
                fcName: 'getHomePageLevel',
                data: {}
            }
            window.android && window.android.postMessage(JSON.stringify(option));
        } else {
            window.ios && window.ios.callHandler("getHomePageLevel", {}, callback);
        }
    },
    // H5页面层级发生改变
    changePageLevel(level) {
        console.log('call changePageLevel:' + level);
        if (AppCommonUtils.browser.android) {
            let option = {
                fcName: 'changePageLevel',
                data: {
                    level
                }
            }
            window.android && window.android.postMessage(JSON.stringify(option));
        } else {
            window.ios && window.ios.callHandler("changePageLevel", {
                level
            }, null);
        }
    },
    // 获取token
    getTMToken(callback) {
        console.log('call getTMToken');
        if (AppCommonUtils.browser.android) {
            window.getTMTokenCallback = callback;
            let option = {
                fcName: 'getTMToken',
                data: {}
            }
            window.android && window.android.postMessage(JSON.stringify(option));
        } else {
            window.ios && window.ios.callHandler("getTMToken", {}, callback);
        }
    },
    // 跳转到登录页面
    goToLogin(callback) {
        console.log('call goToLogin');
        if (AppCommonUtils.browser.android) {
            window.goToLoginCallback = callback;
            let option = {
                fcName: 'goToLogin',
                data: {}
            }
            window.android && window.android.postMessage(JSON.stringify(option));
        } else {
            window.ios && window.ios.callHandler("goToLogin", {}, callback);
        }
    },
    // 获取用户信息
    getTMUserInfo(callback) {
        console.log('call getTMUserInfo');
        if (AppCommonUtils.browser.android) {
            window.getTMUserInfoCallback = callback;
            let option = {
                fcName: 'getTMUserInfo',
                data: {}
            }
            window.android && window.android.postMessage(JSON.stringify(option));
        } else {
            window.ios && window.ios.callHandler("getTMUserInfo", {}, callback);
        }
    },
    // 获取用户定位
    getTMLocationInfo(callback) {
        console.log('call getTMLocationInfo');
        if (AppCommonUtils.browser.android) {
            window.getTMLocationInfoCallback = callback;
            let option = {
                fcName: 'getTMLocationInfo',
                data: {}
            }
            window.android && window.android.postMessage(JSON.stringify(option));
        } else {
            window.ios && window.ios.callHandler("getTMLocationInfo", {}, callback);
        }
    },
    // 获取判断是否登录
    checkTMLogin(callback) {
        console.log('call checkTMLogin');
        if (AppCommonUtils.browser.android) {
            window.checkTMLoginCallback = callback;
            let option = {
                fcName: 'checkTMLogin',
                data: {}
            }
            window.android && window.android.postMessage(JSON.stringify(option));
        } else {
            window.ios && window.ios.callHandler("checkTMLogin", {}, callback);
        }
    },
    // 扫码
    goToQRcode(callback) {
        console.log('call goToQRcode');
        if (AppCommonUtils.browser.android) {
            window.goToQRcodeCallback = callback;
            let option = {
                fcName: 'goToQRcode',
                data: {}
            }
            window.android && window.android.postMessage(JSON.stringify(option));
        } else {
            window.ios && window.ios.callHandler("goToQRcode", {}, callback);
        }
    },
    // 跳转原生组件
    goToNativeComponents(data, callback) {
        console.log('call goToNativeComponents');
        if (AppCommonUtils.browser.android) {
            window.goToNativeComponentsCallback = callback;
            let option = {
                fcName: 'goToNativeComponents',
                data: data
            }
            window.android && window.android.postMessage(JSON.stringify(option));
        } else {
            window.ios && window.ios.callHandler("goToNativeComponents", data, callback);
        }
    },
    // 获取头部信息
    getHeadInfo(callback) {
        if (AppCommonUtils.browser.android) {
            window.getHeadInfoCallback = callback;
            let option = {
                fcName: 'getHeadInfo',
                data: {}
            }
            window.android && window.android.postMessage(JSON.stringify(option));
        } else {
            window.ios && window.ios.callHandler("getHeadInfo", callback);
        }
    },
    // 跳转小程序
    gotoWXMiniProgram (data, callback) {
        console.log('call gotoWXMiniProgram');
        if (AppCommonUtils.browser.android) {
            window.gotoWXMiniProgramCallback = callback;
            let option = {
                fcName: 'gotoWXMiniProgram',
                data: data
            }
            window.android && window.android.postMessage(JSON.stringify(option));
        } else {
            window.ios && window.ios.callHandler("gotoWXMiniProgram", JSON.stringify(data), callback);
        }
    },
    startRecordAudio(data, callback) {
        console.log('call startRecordAudio');
        if (AppCommonUtils.browser.android) {
            window.startRecordAudioCallback = callback;
            let option = {
                fcName: 'startRecordAudio',
                data: data
            }
            window.android && window.android.postMessage(JSON.stringify(option));
        } else {
            window.ios && window.ios.callHandler("startRecordAudio", data, callback);
        }
    },
    // 结束录音
    stopRecordAudio(callback) {
        console.log('call stopRecordAudio');
        if (AppCommonUtils.browser.android) {
            window.stopRecordAudioCallback = callback;
            let option = {
                fcName: 'stopRecordAudio',
                data: {}
            }
            window.android && window.android.postMessage(JSON.stringify(option));
        } else {
            window.ios && window.ios.callHandler("stopRecordAudio", callback);
        }
    },
    // 自动结束录音监控
    callbackRecordAudio(callback) {
        window.TM_AutoStopRecordAudio = function (res) {
            if(typeof res == 'string'){
                callback(res)
            }else{
                callback(JSON.stringify(res));
            }
        }
    },
    TMEncryptionData(data, callback) {
        console.log('call TMEncryptionData');
        if (AppCommonUtils.browser.android) {
            window.encryptionDataCallback = callback;
            let option = {
                fcName: 'encryptionData',
                data: data
            }
            window.android && window.android.postMessage(JSON.stringify(option));
        } else {
            window.ios && window.ios.callHandler("encryptionData", data, callback);
        }
    },
    TMDecryptData(data, callback) {
        console.log('call TMDecryptData');
        if (AppCommonUtils.browser.android) {
            window.decryptDataCallback = callback;
            let option = {
                fcName: 'decryptData',
                data: data
            }
            window.android && window.android.postMessage(JSON.stringify(option));
        } else {
            window.ios && window.ios.callHandler("decryptData", data, callback);
        }
    },
    TMOpenBrowser(data, callback) {
        console.log('call TMOpenBrowser');
        if (AppCommonUtils.browser.android) {
            window.openBrowserCallback = callback;
            let option = {
                fcName: 'openBrowser',
                data: data
            }
            window.android && window.android.postMessage(JSON.stringify(option));
        } else {
            window.ios && window.ios.callHandler("openBrowser", data, callback);
        }
    },
    tmScreenSecure(data, callback) {
        console.log('call tmScreenSecure');
        if (AppCommonUtils.browser.android) {
            window.tmScreenSecureCallback = callback;
            let option = {
                fcName: 'tmScreenSecure',
                data: data
            }
            window.android && window.android.postMessage(JSON.stringify(option));
        } else {
            window.ios && window.ios.callHandler("tmScreenSecure", data, callback);
        }
    },
    tmJumpNavigationAPP(data,callback) {
        console.log('call tmJumpNavigationAPP');
        if(AppCommonUtils.browser.android){
            window.tmJumpNavigationAPPCallback = callback;
            let option = {
                fcName: 'tmJumpNavigationAPP',
                data: data
            }
            window.android && window.android.postMessage(JSON.stringify(option));
        }else{
            window.ios && window.ios.callHandler("tmJumpNavigationAPP", data,callback);
        }
    },
    tmBecomeActiveShowPage(data,callback) {
        console.log('call tmBecomeActiveShowPage');
        if(AppCommonUtils.browser.android){
            window.tmBecomeActiveShowPageCallback = callback;
            let option = {
                fcName: 'tmBecomeActiveShowPage',
                data: data
            }
            window.android && window.android.postMessage(JSON.stringify(option));
        }else{
            window.ios && window.ios.callHandler("tmBecomeActiveShowPage", data,callback);
        }
    },
    createShareFunctionButton(data,callback) {
        console.log('call createShareFunctionButton');
        if(AppCommonUtils.browser.android){
            window.createShareFunctionButtonCallback = callback;
            let option = {
                fcName: 'createShareFunctionButton',
                data: data
            }
            window.android && window.android.postMessage(JSON.stringify(option));
        }else{
            window.ios && window.ios.callHandler("createShareFunctionButton", data,callback);
        }
    }
};